package uodm.client;

import java.util.List;

import uodm.shared.model.OfficeData;
import uodm.shared.rpc.UodmService;
import uodm.shared.rpc.UodmServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class ListOfficesEntry implements EntryPoint {

	UodmServiceAsync uodmService = GWT.create(UodmService.class);

	private ListOfficesWidget mainWidget;

	@Override
	public void onModuleLoad() {
		mainWidget = new ListOfficesWidget(this);
		RootPanel.get("main").add(mainWidget);
		loadOfficeList();
	}

	public void loadOfficeList() {
		uodmService.getOfficeDataList(new AsyncCallback<List<OfficeData>>() {

			@Override
			public void onSuccess(List<OfficeData> result) {
				mainWidget.messageLabel.setText("");
				for (int i = 0; i < result.size(); ++i) {
					mainWidget.officeList.setText(i, 0, String.valueOf(i));
					OfficeData od = result.get(i);
					mainWidget.officeList.setText(i, 1, od.getName());
					mainWidget.officeList.setText(i, 2, od.getCreatorEmail());
					mainWidget.officeList.setText(i, 3, od.getNote());
				}
				mainWidget.messageLabel.setText("Successful");
			}

			@Override
			public void onFailure(Throwable caught) {
				mainWidget.messageLabel.setText("Failed: "
						+ caught.getMessage());
			}
		});
	}

	public void redirectToAddOffice() {
		Window.Location.assign("/addoffice.jsp");
	}
}
